package com.crrc.yzt.demo.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.crrc.yzt.demo.blue.R
import com.crrc.yzt.demo.adapter.CarDetailAdapter
import com.crrc.yzt.demo.base.BaseActivity
import com.crrc.yzt.demo.blue.databinding.ActivityMoreDetaiBinding
import com.crrc.yzt.demo.utils.StatusBarUtils.setStatusBarBackground
import com.crrc.yzt.demo.vm.CarVM
import kotlinx.coroutines.launch

class MoreDetaiActivity : BaseActivity() {

    private lateinit var binding: ActivityMoreDetaiBinding

    private var carDetailAdapter: CarDetailAdapter? = null

    private val viewModel by viewModels<CarVM>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStatusBarBackground(R.color.colorPrimary)
        binding = ActivityMoreDetaiBinding.inflate(layoutInflater)
        setContentView(binding.root)

        carDetailAdapter = CarDetailAdapter(this)

        binding.expandableListView.setAdapter(carDetailAdapter)
        binding.expandableListView.expandGroup(0)
        binding.expandableListView.expandGroup(1)
        binding.expandableListView.expandGroup(2)

        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.tvMore.setOnClickListener {
            Toast.makeText(mActivity,"success",Toast.LENGTH_SHORT).show()
        }

        viewModel.loadCarDetail()

        lifecycleScope.launch {
            viewModel.carDetailStateFlow.collect{
                carDetailAdapter?.apply {
                    carDetail = it
                    notifyDataSetChanged()
                }
            }
        }


    }
}